package com.shrikanthravi.customnavigationdrawer;

import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.*;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: Custom-Navigation-Drawer-master
 * @description:
 * @author:
 * @create: 2020-12-25 09:02
 **/
public class Function {
    /**
     * 得到当前的手机网络类型
     * @param context
     * @return
     */
    public static String getCurrentNetType(Context context) {
        String type = null;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null) {
            type = "null";
        } else if (((NetworkInfo) info).getType() == ConnectivityManager.TYPE_WIFI) {
            type = "wifi";
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            int subType = info.getSubtype();
            if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
                    || subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                type = "2g";
            } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
                type = "3g";
            } else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
                type = "4g";
            }
        }
        return type;
    }

    /**
     * 获取屏幕亮度
     * @param context
     * @return
     */
    public static int getScreenBrightness(Context context) {
        int value = 0;
        ContentResolver cr = context.getContentResolver();
        try {
            value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {

        }
        return value;
    }

    /**
     * 获取手机电量
     * @param context
     * @return
     */
    public static int getPhone_electric(Context context){
        BatteryManager manager = (BatteryManager)context.getSystemService(context.BATTERY_SERVICE);
        int phone_electric = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        return phone_electric;
    }

    public static int get_Delay() {
        String delay = new String();
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("ping " + "baidu.com");
            BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuffer str = new StringBuffer();
            String line = null;
            while ((line = buf.readLine()) != null) {
                str.append(line);
            }
            if (str.toString().contains("平均")) {
                int i = str.indexOf("平均"+5);
                System.out.println("延迟:" + str.substring(i, str.length()));
                delay = str.substring(i, str.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        int s = Integer.valueOf(delay);
        return s;
    }
    // 判断网络状态
    public static String isConnect() {
        BufferedReader br = null;
        try{
            Process process = Runtime.getRuntime().exec("ping www.baidu.com");
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(),"GBK");
            br = new BufferedReader(inputStreamReader);
            String line =null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null){
                System.out.println(line);
                sb.append(line+"\n");
                line=br.readLine();
            }
            if(!sb.toString().contains("平均")){
                return "无网络";
            }
            else{
                return sb.toString().substring(sb.toString().lastIndexOf("平均")+5,sb.length());
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return "获取失败";
    }

}
